package com.sun.umbrella.session;



/**
 * @author Jay
 * @version v1.0
 * @description 用户的session信息
 * @date 2017-11-24 15:35
 */

public class ContextHolder {

    private ThreadLocal<SessionContext> sessionContext = new ThreadLocal<>();


    public void initSessionContext(Integer userId, String userName, Integer roleId, String roleNo, String roleName) {
        if (isSessionContextInitialized()) {
            throw new IllegalStateException("duplicated initialization of sessionContext,check if this method invoked somewhere!!");
        }
        sessionContext.set(new SessionContext(userId, userName));
    }

    public void initSessionContext(SessionContext sessionContext) {
        if (isSessionContextInitialized()) {
            throw new IllegalStateException("duplicated initialization of sessionContext,check if this method invoked somewhere!!");
        }
        this.sessionContext.set(sessionContext);
    }

    public boolean isSessionContextInitialized() {
        return (sessionContext != null) && (sessionContext.get() != null) && (sessionContext.get().getUserId() != null);
    }

    public SessionContext getSessionContext() {
        if (!isSessionContextInitialized()) {
            initSessionContext(null, null, null, null, null);
        }
        return sessionContext.get();
    }

    public void resetContext() {
        sessionContext.remove();
    }
}
